package server.business.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import server.business.pojo.*;
import server.business.service.*;
import server.business.utils.GenerateNumberBySnowFlake;
import server.business.utils.JsonResult;
import server.business.utils.results.lrz.Result;

import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;
import java.util.ArrayList;
import java.util.List;

//myq
@RestController
@RequestMapping("/transfusionAutologous")
public class TransfusionAutologousController {
    @Autowired
    private TransfusionAutologousService transfusionAutologousService;
    @Autowired
    private DiseaseTypeService diseaseTypeService;
    @Autowired
    private TransfusionReviewService transfusionReviewService;
    @Autowired
    private BloodTestService bloodTestService;
    @Autowired
    private PatientService patientService;

    // *************************************myq*************************start
    /**
     * 页面展示自体输血申请记录
     *
     * @return
     */
    @RequestMapping("/normal/list")
    @ResponseBody
    public JsonResult<Object> listPage() {
        Page<TransfusionReview> pageList = transfusionReviewService.getAutoInfo();
        return JsonResult.success(pageList);
    }

    /**
     * 自体输血申请
     *
     * @param transfusionAutologous
     * @param transfusionReview
     * @return
     */
    @RequestMapping(value = "/normal/addformdata", method = RequestMethod.POST)
    @ResponseBody
    public JsonResult<Object> saveAutoApplication(@RequestBody @Valid TransfusionAutologous transfusionAutologous,
                                                  TransfusionReview transfusionReview,@RequestParam(name = "applicantId") String applicantId) {
        long transfusionNumberId = Long.parseLong(transfusionAutologous.getIdentifyRho());
        GenerateNumberBySnowFlake transfusionNumber1 = new GenerateNumberBySnowFlake(transfusionNumberId, "A");
        transfusionAutologous.setTransfusionNumber(transfusionNumber1.genNumberByflag());
        System.out.println(transfusionNumber1.genNumberByflag());
        transfusionAutologousService.tranAutoAdd(transfusionAutologous);
        String transfusionNumber = transfusionAutologous.getTransfusionNumber();
        transfusionReviewService.insertRecord(transfusionNumber);
        transfusionReview.setState("1");
        transfusionReviewService.updateReview(transfusionNumber, transfusionReview);
        return JsonResult.success();

    }


    /**
     * 自体输血申请审批
     */
    @RequestMapping(value = "/review/pass", method = RequestMethod.POST)
    @ResponseBody
    public JsonResult<Object> reviewAutoApplication(@RequestBody @Valid TransfusionAutologous transfusionAutologous,
                                                    TransfusionReview transfusionReview, @RequestParam(name = "reviewerId") String reviewerId) {
        String transfusionNumber = transfusionAutologous.getTransfusionNumber();
        System.out.println(reviewerId);
        transfusionReview.setState("2");
        transfusionReviewService.updateReview(transfusionNumber, transfusionReview);
        return JsonResult.success();
    }

    /**
     * 点击审核展示输血申请单信息(只根据输血申请单号)
     */
    @PostMapping("/normal/applyList")
    @ResponseBody
    public JsonResult<Object> getAutoInfo(HttpServletRequest httpServletRequest) {
        String transfusionNumber = httpServletRequest.getParameter("transfusionNumber");
        System.out.println(transfusionAutologousService.getAutoInfo(transfusionNumber));
        ArrayList<Object> listOne = transfusionAutologousService.getAutoInfo(transfusionNumber);
        ArrayList<Object> listTwo = bloodTestService.getTestInfo(transfusionNumber);
        listOne.addAll(listTwo);
        System.out.println(listOne);
        return JsonResult.success(listOne);
    }

    /**
     * 自体输血申请驳回
     */
    @RequestMapping(value = "/review/reject", method = RequestMethod.POST)
    @ResponseBody
    public JsonResult<Object> clinicReject(
            @RequestBody(required = false) @Valid TransfusionAutologous transfusionAutologous,
            TransfusionReview transfusionReview, @RequestParam(name = "rejectReason") String rejectReason) {
        String transfusionNumber = transfusionAutologous.getTransfusionNumber();
        System.out.println(transfusionNumber);
        transfusionReview.setState("3");
        transfusionReviewService.updateReview(transfusionNumber, transfusionReview);
        System.out.println(rejectReason);
        return JsonResult.success();
    }

    /**
     * 获取临床诊断数据字典
     *
     * @return
     */
    @GetMapping("/normal/diagnosis")
    @ResponseBody
    public List<DiseaseType> getDiagnosisInfo() {
        return diseaseTypeService.list();
    }

    /**
     * 分页查询
     */
    @GetMapping("/normal/{currentPage}/{pageSize}")
    @ResponseBody
    public JsonResult<Object> getPage(@PathVariable int currentPage, @PathVariable int pageSize,
                                      TransfusionReview transfusionReview, String startTime, String endTime, String reviewStartTime,
                                      String reviewEndTime) {
        IPage<TransfusionReview> page = transfusionReviewService.getSelectAutoPage(currentPage, pageSize,
                transfusionReview, startTime, endTime, reviewStartTime, reviewEndTime);
        if (currentPage > page.getPages()) {
            page = transfusionReviewService.getSelectAutoPage((int) page.getPages(), pageSize, transfusionReview,
                    startTime, endTime, reviewStartTime, reviewEndTime);
        }
        return JsonResult.success(page);
    }
    /**
     * 输入病历号自动回填身份证号（自体输血申请）
     */
    @RequestMapping(value = "/normal/getIdcard",method = RequestMethod.GET)
    @ResponseBody
    public JsonResult<Object> getIdcard(HttpServletRequest httpServletRequest){
        System.out.println(httpServletRequest.getParameter("medicalRecordNumber"));
        String medicalRecordNumber=httpServletRequest.getParameter("medicalRecordNumber");

        System.out.println(patientService.getIdcard(medicalRecordNumber));
        return JsonResult.success(patientService.getIdcard(medicalRecordNumber));
    }

    /**
     * 批量删除
     */
    @RequestMapping("/normal/batchDelete")
    @ResponseBody
    public JsonResult<Object> batchDelete(@RequestBody ArrayList<Long> ids) {
        System.out.println(ids);
        ArrayList<Long> transfusionIds = transfusionAutologousService.selectId(ids);
        transfusionAutologousService.removeByIds(transfusionIds);
        boolean success = transfusionReviewService.removeByIds(ids);
        if (success) {
            return JsonResult.success();
        } else {
            return JsonResult.error();
        }
    }
    // *************************************myq*************************end

    // *************************************lrz*************************start
    @GetMapping("/normal/lrz/{currentPage}/{pageSize}")
    @ResponseBody
    public Result getPage1(@PathVariable int currentPage, @PathVariable int pageSize,
            TransfusionAutologous transfusionAutologous) {
        IPage<TransfusionAutologous> page = transfusionAutologousService.getPage1(currentPage, pageSize,
                transfusionAutologous);
        // 如果当前页码值大于了总页码值，就重新执行查询操作，使用最大页码值作为当前页码值
        if (currentPage > page.getPages()) {
            page = transfusionAutologousService.getPage1((int) page.getPages(), pageSize, transfusionAutologous);
        }
        return Result.success(page);
    }

    /**
     * 批量删除
     *
     * @param ids
     * @return
     */
    @RequestMapping("/normal/batchDelete1")
    @ResponseBody
    public Result batchDelete1(@RequestBody ArrayList<Long> ids) {
        System.out.println(ids);
        boolean success = transfusionAutologousService.removeByIds(ids);
        Result result = new Result();
        if (success) {
            result.setCode("200");
            result.setMsg("删除成功");
        } else {
            result.setCode("500");
            result.setMsg("删除失败");
        }
        return result;
    }
    // *************************************lrz*************************end

}
